import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AuthService } from '../../services/admin/auth.service';
import { AuthSiteService } from '../../services/admin/auth-site.service';

/**
 * AuthController
 * 对应 PHP: Auth Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('auth')
@Controller('adminapi/auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly authSiteService: AuthSiteService,
  ) {}

  /**
   * 用户管理
   * 路由: GET authmenu
   * PHP路由: Route::get('authmenu', 'auth.Auth/authMenuList')
   */
  @Get('authmenu')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async authMenuList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: authMenuList

      return await this.authService.getAuthMenuList(1, data['addon']);
    } catch (error) {
      throw new BusinessException('authMenuList操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: GET authaddon
   * PHP路由: Route::get('authaddon', 'auth.Auth/getAuthAddonList')
   */
  @Get('authaddon')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async getAuthAddonList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getAuthAddonList

      return await this.authSiteService.getAuthAddonList();
    } catch (error) {
      throw new BusinessException('getAuthAddonList操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: GET get
   * PHP路由: Route::get('get', 'auth.Auth/get')
   */
  @Get('get')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async get(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: get

      return await this.authService.getAuthInfo(id);
    } catch (error) {
      throw new BusinessException('get操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: PUT modify/:field
   * PHP路由: Route::put('modify/:field', 'auth.Auth/modify')
   */
  @Put('modify/:field')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async modify(
    @Param('field') field: string,
    @Body() data: ModifyDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: modify

      return await this.authService.modifyAuth(field, data['value']);
    } catch (error) {
      throw new BusinessException('modify操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: PUT edit
   * PHP路由: Route::put('edit', 'auth.Auth/edit')
   */
  @Put('edit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async edit(@Body() data: EditDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.authService.editAuth(data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: GET site
   * PHP路由: Route::get('site', 'auth.Auth/site')
   */
  @Get('site')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async site(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: site

      return await this.authSiteService.getSiteInfo(id);
    } catch (error) {
      throw new BusinessException('site操作失败', error);
    }
  }

  /**
   * 用户管理
   * 路由: GET site/showmenu
   * PHP路由: Route::get('site/showmenu', 'auth.Auth/getShowMenuList')
   */
  @Get('site/showmenu')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '用户管理' })
  async getShowMenuList(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getShowMenuList

      return await this.authSiteService.getShowMenuList();
    } catch (error) {
      throw new BusinessException('getShowMenuList操作失败', error);
    }
  }
}
